//
//  Row.m
//  
//  jhf
//
//  Created by 计海峰 on 2019/6/28.
//  Copyright © 2019 计海峰. All rights reserved.
//

#import "Row.h"
#import "BindProtocol.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <CooperSDK/UIColor+BFPaperColors.h>

@interface Row ()

@property (nonatomic, strong, readwrite) Class cellClass;

@property (nonatomic, copy, readwrite) id identify;

@end

@implementation Row

+ (NSBundle *)table_refreshBundle {
    static NSBundle *refreshBundle = nil;
    if (refreshBundle == nil) {
        // 这里不使用mainBundle是为了适配pod 1.x和0.x
        refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[Row class]] pathForResource:@"Cooper_Table" ofType:@"bundle"]];
    }
    return refreshBundle;
}


- (UITableViewCell * _Nonnull (^)(NSIndexPath * _Nonnull, UITableView * _Nonnull))configCellBlock {
    if (!_configCellBlock) {
        @weakify(self)
        _configCellBlock = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
            @strongify(self)
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier];
            // 未注册
            if (cell == nil) {
                NSBundle *bundle;
                if (self.bundle) {
                    bundle = self.bundle;
                }else {
                    bundle = [NSBundle mainBundle];
                }
                NSString *xibPath = [bundle pathForResource:self.reuseIdentifier ofType:@"nib"];
                if (xibPath && xibPath.length > 0) { // 先试用外部的
                    [tableView registerNib:[UINib nibWithNibName:self.reuseIdentifier bundle:bundle]
                    forCellReuseIdentifier:self.reuseIdentifier];
                } else { // 尝试内部
//                    NSBundle *bundle = [NSBundle bundleForClass:self.cellClass];
                    NSBundle *bundle =[Row table_refreshBundle];
                    NSString *xibPath = [bundle pathForResource:self.reuseIdentifier ofType:@"nib"];
                    if (xibPath && xibPath.length > 0) {
                        [tableView registerNib:[UINib nibWithNibName:self.reuseIdentifier bundle:bundle]
                        forCellReuseIdentifier:self.reuseIdentifier];
                    }else { // 最后尝试class
                        [tableView registerClass:self.cellClass forCellReuseIdentifier:self.reuseIdentifier];
                    }
                }
                cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier forIndexPath:indexPath];
            }
            // 协议
            if ([cell conformsToProtocol:@protocol(BindProtocol)]
                && [cell respondsToSelector:@selector(bindTo:)] ) {
                [cell performSelector:@selector(bindTo:)
                           withObject:self.model];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (self.bgColorHex) {
                if (self.bgColorHex.length == 7) {
                    cell.backgroundColor = [UIColor colorWithHexString:self.bgColorHex];
                }else {
                    cell.backgroundColor = [UIColor clearColor];
                }
            }
            if (self.height == 0) {
                cell.hidden = true;
            }else {
                cell.hidden = false;
            }
            return cell;
        };
    }
    return _configCellBlock;
}

// MARK:- identify初始化 直接显示
+ (instancetype)rowIdentify:(NSString *)identify; {
    return [[self alloc] initWithIdentify:identify];
}

- (instancetype)initWithIdentify:(NSString *)identify
{
    self = [super init];
    if (self) {
        self.identify = identify;
        self.height = UITableViewAutomaticDimension;
    }
    return self;
}

// MARK:- class初始化

+ (instancetype)rowClassStr:(NSString *)clsStr model:(id)model {
    return [[self alloc] initWithClass:NSClassFromString(clsStr) model:model];
}

+ (instancetype)rowClassStr:(NSString *)clsStr {
    return [[self alloc] initWithClass:NSClassFromString(clsStr) model:nil];
}

+ (instancetype)rowClass:(Class)cls {
    return [[self alloc] initWithClass:cls model:nil];
}

+ (instancetype)rowClass:(Class)cls model:(id)model {
    return [[self alloc] initWithClass:cls model:model];
}

- (instancetype)initWithClass:(Class)cls model:(id)model {
    if (self = [super init]) {
        self.cellClass = cls;
        self.model = model;
        self.height = UITableViewAutomaticDimension;
    }
    return self;
}

- (NSString *)reuseIdentifier {
    if (self.identify) {
        return self.identify;
    }
    return [NSString stringWithFormat:@"%@", self.cellClass];
}


- (id)modelClass:(Class)cls; {
    if (self.model && [self.model isKindOfClass:BindModel.class]) {
        id model = ((BindModel *)self.model).model;
        if (model && [model isKindOfClass:cls]) {
            return model;
        }
    }
    return nil;
}

@end
